package com.example.pro1.controller.lian;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pro1.entity.lian.*;
import com.example.pro1.entity.lian.pojoMid.*;
import com.example.pro1.service.lian.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/root")
@CrossOrigin
public class rootController {
    @Autowired
    GoodpreService GoodpreService;

    @Autowired
    EvaluationService EvaluationService;

    @Autowired
    com.example.pro1.controller.lian.customerController customerController;

    @Autowired
    GorderService GorderService;

    @Autowired
    CustomerService CustomerService;

    @Autowired
    GoodService GoodService;


    @Autowired
    GoodtogfsService GoodtogfsService;

    @Autowired
    GoodsecondsortService GoodsecondsortService;

    @Autowired
    GsstogfsService GsstogfsService;

    @Autowired
    GoodfirstsortService GoodfirstsortService;


    /*1商品管理*/
    /*1.1修改商品信息*/
    @RequestMapping("/updateGoodInfo")
    public Boolean updateGoodInfo(
            @RequestParam("gId")String gId,
            @RequestParam("gssId")String newGssId,
            @RequestParam("gfsId")String newGfsId,
            @RequestParam("gName")String gName,
            @RequestParam("gNumber")String gNumber,
            @RequestParam("gInprince")String gInPrince,
            @RequestParam("gOutprince")String gOutPrince,
            @RequestParam("gOutNum")String gOutNum,
            @RequestParam("gSalePrice")String gSalePrice,
            @RequestParam("comments")String comments,
            @RequestParam("gImg")String gImg
    ){
        LambdaUpdateWrapper<Good> GoodUpdateWrapper = new LambdaUpdateWrapper<>();
        GoodUpdateWrapper
                .eq(StringUtils.isNotBlank(gId),Good::getGid,gId)
                .set(Good::getGname,gName)
                .set(Good::getGnumber,gNumber)
                .set(Good::getGinprince,gInPrince)
                .set(Good::getGoutprince,gOutPrince)
                .set(Good::getGoutnum,gOutNum)
                .set(Good::getGsaleprice,gSalePrice)
                .set(Good::getComments,comments)
                .set(Good::getGimg,gImg);
        /*更新商品的基本信息*/
        boolean update = GoodService.update(GoodUpdateWrapper);
        /*先根据gId查询旧的gssId*/
        LambdaQueryWrapper<Goodtogfs> GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
        GoodtogfsQueryWrapper.eq(StringUtils.isNotBlank(gId),Goodtogfs::getGid,gId);
        Goodtogfs goodtogfs = GoodtogfsService.getOne(GoodtogfsQueryWrapper);
        if (goodtogfs!=null){
//            String oldGssid = goodtogfs.getGssid();
            /*删除商品和二级分类的对应关系*/
            GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
            GoodtogfsQueryWrapper
//                .eq(StringUtils.isNotBlank(oldGssid),Goodtogfs::getGssid,oldGssid)
                    .eq(StringUtils.isNotBlank(gId),Goodtogfs::getGid,gId);
            GoodtogfsService.remove(GoodtogfsQueryWrapper);
        }
        /*添加新的商品和二级分类的关系*/
        goodtogfs = new Goodtogfs(
                gId,
                newGssId
        );
        return GoodtogfsService.save(goodtogfs);
        /*根据oldGssid查询旧的gfsId*/
//        LambdaQueryWrapper<Gsstogfs> GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
//        GsstogfsQueryWrapper.eq(StringUtils.isNotBlank(oldGssid),Gsstogfs::getGssid,oldGssid);
//        Gsstogfs gsstogfs = GsstogfsService.getOne(GsstogfsQueryWrapper);

    }

    /*1.2获取商品总数*/
    @RequestMapping("/getGoodsNum")
    public Long getGoodNum(){
        return GoodService.count();
    }

    /*1.3分页查询商品信息(包括分类信息和)*/
    @RequestMapping("/getGoodInfoPage")
    public List<goodWithSort> getGoodInfoPage(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        /*定义返回列表信息*/
        List<goodWithSort> returnList = new ArrayList<>();
        Page<Good> GoodPage = new Page<>(pageOrder,itemNum);
        LambdaQueryWrapper<Good> GoodQueryWrapper = new LambdaQueryWrapper<>();
        GoodQueryWrapper.ne(true,Good::getGnumber,-1);
        Page<Good> page = GoodService.page(GoodPage, GoodQueryWrapper);
        List<Good> goodList = page.getRecords();
        for (Good good : goodList) {
            /*根据gId查询gssId*/
            String gid = good.getGid();
            LambdaQueryWrapper<Goodtogfs> GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
            GoodtogfsQueryWrapper
                    .eq(StringUtils.isNotBlank(gid),Goodtogfs::getGid,gid);
            Goodtogfs goodtogfs = GoodtogfsService.getOne(GoodtogfsQueryWrapper);
            String gssid = goodtogfs.getGssid();
            /*根据gssId查询gfsId*/
            LambdaQueryWrapper<Gsstogfs> GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
            GsstogfsQueryWrapper
                    .eq(StringUtils.isNotBlank(gssid),Gsstogfs::getGssid,gssid);
            Gsstogfs gsstogfs = GsstogfsService.getOne(GsstogfsQueryWrapper);
            String gfsid = gsstogfs.getGfsid();
            /*根据gssId查询二级分类信息*/
            Goodsecondsort goodsecondsort = GoodsecondsortService.getById(gssid);
            /*根据gfsId查询一级分类信息*/
            Goodfirstsort goodfirstsort = GoodfirstsortService.getById(gfsid);
            goodWithSort goodWithSort = new goodWithSort(
                    good,
                    goodfirstsort,
                    goodsecondsort
            );
            returnList.add(goodWithSort);
        }
        return returnList;
    }


    /*1.31*/
    /*根据商品名字查询信息*/
    @RequestMapping("/getGoodLike")
    public List<goodWithSort> getGoodLike(
            @RequestParam("gName") String gName
    ){
        /*定义返回列表信息*/
        List<goodWithSort> returnList = new ArrayList<>();
        /*根据商品名称查询商品信息*/
        LambdaQueryWrapper<Good> GoodQueryWrapper = new LambdaQueryWrapper<>();
        GoodQueryWrapper
                .like(StringUtils.isNotBlank(gName),Good::getGname,gName);
        List<Good> goodList = GoodService.list(GoodQueryWrapper);
        for (Good good : goodList) {
            /*根据gId查询gssId*/
            String gid = good.getGid();
            LambdaQueryWrapper<Goodtogfs> GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
            GoodtogfsQueryWrapper
                    .eq(StringUtils.isNotBlank(gid),Goodtogfs::getGid,gid);
            Goodtogfs goodtogfs = GoodtogfsService.getOne(GoodtogfsQueryWrapper);
            String gssid = goodtogfs.getGssid();
            /*根据gssId查询gfsId*/
            LambdaQueryWrapper<Gsstogfs> GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
            GsstogfsQueryWrapper
                    .eq(StringUtils.isNotBlank(gssid),Gsstogfs::getGssid,gssid);
            Gsstogfs gsstogfs = GsstogfsService.getOne(GsstogfsQueryWrapper);
            String gfsid = gsstogfs.getGfsid();
            /*根据gssId查询二级分类信息*/
            Goodsecondsort goodsecondsort = GoodsecondsortService.getById(gssid);
            /*根据gfsId查询一级分类信息*/
            Goodfirstsort goodfirstsort = GoodfirstsortService.getById(gfsid);
            goodWithSort goodWithSort = new goodWithSort(
                    good,
                    goodfirstsort,
                    goodsecondsort
            );
            returnList.add(goodWithSort);
        }
        return returnList;
    }

    /*1.4.添加商品信息*/
    @RequestMapping("/saveGood")
    public Boolean saveGood(
            @RequestParam("gfsId")String newGfsId,
            @RequestParam("gssId")String newGssId,
            @RequestParam("gName")String gName,
            @RequestParam("gNumber")String gNumber,
            @RequestParam("gInprince")String gInPrince,
            @RequestParam("gOutprince")String gOutPrince,
            @RequestParam("gOutNum")String gOutNum,
            @RequestParam("gSalePrice")String gSalePrice,
            @RequestParam("comments")String comments,
            @RequestParam("gImg")String gImg
    ){
        /*添加商品信息，获取gId*/
        Good good = new Good(
                null,
                gName,
                gNumber,
                gInPrince,
                gOutPrince,
                gOutNum,
                gSalePrice,
                comments,
                gImg
        );
        boolean saveGood = GoodService.save(good);
        String gid = good.getGid();
        /*将gid与gssId插入对应表中*/
        Goodtogfs goodtogfs = new Goodtogfs(
                gid,
                newGssId
        );
        boolean saveSort = GoodtogfsService.save(goodtogfs);
        return saveSort&&saveGood;
    }

    /*修改*/
    /*1.5根据gId删除商品信息*/
    @RequestMapping("/deleteGoodByGId")
    public Boolean deleteGoodByGId(
            @RequestParam("gId")String gId

    ){
        /*根据gId与gssId删除分类信息*/
        LambdaQueryWrapper<Goodtogfs> GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
        GoodtogfsQueryWrapper
                .eq(StringUtils.isNotBlank(gId),Goodtogfs::getGid,gId);
        boolean remove = GoodtogfsService.remove(GoodtogfsQueryWrapper);
        /*根据gId删除商品信息*/
        /*将商品信息的库存更改为-1*/
        LambdaUpdateWrapper<Good> GoodUpdateWrapper = new LambdaUpdateWrapper<>();
        GoodUpdateWrapper
                .eq(StringUtils.isNotBlank(gId),Good::getGid,gId)
                .set(Good::getGnumber,-1);
        return GoodService.update(GoodUpdateWrapper);
    }

    /*2分类管理*/

    /*2.01根据一级分类名查询*/

    /*2.1新增一级分类*/
    @RequestMapping("/addGfsSort")
    public Boolean addGfsSort(
            @RequestParam("gfsName")String gfsName,
            @RequestParam("gfsImageSrc")String gfsImageSrc
    ){
        Goodfirstsort Goodfirstsort = new Goodfirstsort(
                null,
                gfsName,
                gfsImageSrc
        );
        return GoodfirstsortService.save(Goodfirstsort);
    }

    /*2.2新增二级分类*/
    @RequestMapping("/addGssSort")
    public Boolean addGssSort(
            @RequestParam("gssName")String gssName,
            @RequestParam("gssImageSrc")String gssImageSrc,
            @RequestParam("gfsId")String gfsId
    ){
        Goodsecondsort Goodsecondsort = new Goodsecondsort(
                null,
                gssName,
                gssImageSrc
        );
        boolean save = GoodsecondsortService.save(Goodsecondsort);
        /*添加和一级分类的关系*/
        Gsstogfs Gsstogfs = new Gsstogfs(Goodsecondsort.getGssid(),gfsId);
        boolean save1 = GsstogfsService.save(Gsstogfs);
        return save&&save1;

    }

    /*2.3修改一级分类*/
    @RequestMapping("/updateGoodFirstSort")
    public Boolean updateGoodFirstSort(
            @RequestParam("gfsId")String gfsId,
            @RequestParam("gfsName")String gfsName,
            @RequestParam("gfsImageSrc")String gfsImageSrc
    ){
        LambdaUpdateWrapper<Goodfirstsort> GoodfirstsortUpdateWrapper = new LambdaUpdateWrapper<>();
        GoodfirstsortUpdateWrapper
                .eq(StringUtils.isNotBlank(gfsId),Goodfirstsort::getGfsid,gfsId)
                .set(Goodfirstsort::getGfsname,gfsName)
                .set(Goodfirstsort::getGfsimagesrc,gfsImageSrc);
        return GoodfirstsortService.update(GoodfirstsortUpdateWrapper);
    }

    /*2.4修改二级分类*/
    @RequestMapping("/updateGoodSecondSort")
    public Boolean updateGoodSecondSort(
            @RequestParam("gssId")String gssId,
            @RequestParam("gssName")String gssName,
            @RequestParam("gssImageSrc")String gssImageSrc,
            @RequestParam("gfsId")String gfsId
    ){
        LambdaUpdateWrapper<Goodsecondsort> GoodsecondsortUpdateWrapper = new LambdaUpdateWrapper<>();
        GoodsecondsortUpdateWrapper
                .eq(StringUtils.isNotBlank(gssId),Goodsecondsort::getGssid,gssId)
                .set(Goodsecondsort::getGssname,gssName)
                .set(Goodsecondsort::getGssimagesrc,gssImageSrc);
        GoodsecondsortService.update(GoodsecondsortUpdateWrapper);
        /*根据gssId修改二级分类与一级分类的关系*/
        /*先删除二级分类与一级分类的关系*/
        LambdaQueryWrapper<Gsstogfs> GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
        GsstogfsQueryWrapper
                .eq(StringUtils.isNotBlank(gssId),Gsstogfs::getGssid,gssId);
        boolean remove = GsstogfsService.remove(GsstogfsQueryWrapper);
        /*添加一级分类和二级分类的关系*/
        Gsstogfs Gsstogfs = new Gsstogfs(gssId,gfsId);
        return GsstogfsService.save(Gsstogfs)&&remove;
    }

    /*修改当中*/
    /*2.5删除一级分类(包括其下面的二级分类与商品，慎用)*/
    @RequestMapping("/deleteFirstSort")
    public Boolean deleteFirstSort(
            @RequestParam("gfsId")String gfsId
    ){
        /*根据一级分类id查询所有对应的二级分类id*/
        LambdaQueryWrapper<Gsstogfs> GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
        GsstogfsQueryWrapper
                .eq(StringUtils.isNotBlank(gfsId),Gsstogfs::getGfsid,gfsId);
        List<Gsstogfs> gsstogfsList = GsstogfsService.list(GsstogfsQueryWrapper);
        for (Gsstogfs gsstogfs : gsstogfsList) {
            String gssid = gsstogfs.getGssid();
            GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
            GsstogfsQueryWrapper
                    .eq(StringUtils.isNotBlank(gssid),Gsstogfs::getGssid,gssid)
                    .eq(StringUtils.isNotBlank(gfsId),Gsstogfs::getGfsid,gfsId);
            boolean remove = GsstogfsService.remove(GsstogfsQueryWrapper);
            /*删除二级分类信息*/
            Boolean remove2 = deleteSecondSort(gssid);
        }
        /*删除一级级分类信息*/
        return GoodfirstsortService.removeById(gfsId);
    }

    /*修改当中*/
    /*2.6删除二级分类(包括下面的商品，慎用)*/
    @RequestMapping("/deleteSecondSort")
    public Boolean deleteSecondSort(
            @RequestParam("gssId")String gssId
    ){
        /*根据gssId查询所有对应商品的信息*/
        LambdaQueryWrapper<Goodtogfs> GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
        GoodtogfsQueryWrapper
                .eq(StringUtils.isNotBlank(gssId),Goodtogfs::getGssid,gssId);
        /*删除二级分类与商品连接信息*/
        boolean remove = GoodtogfsService.remove(GoodtogfsQueryWrapper);
        /*删除二级分类与一级商品连接信息*/
        LambdaQueryWrapper<Gsstogfs> GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
        GsstogfsQueryWrapper
                .eq(StringUtils.isNotBlank(gssId),Gsstogfs::getGssid,gssId);
        boolean remove1 = GsstogfsService.remove(GsstogfsQueryWrapper);

        /*删除二级分类信息*/
//        boolean b = GoodsecondsortService.removeById(gssId);
//        List<Goodtogfs> goodtogfsList = GoodtogfsService.list(GoodtogfsQueryWrapper);


//        for (Goodtogfs goodtogfs : goodtogfsList) {
//            /*获取gId*/
//            String gid = goodtogfs.getGid();
//            /*先删除分类对应表中的信息*/
//            GoodtogfsQueryWrapper = new LambdaQueryWrapper<>();
//            GoodtogfsQueryWrapper
//                    .eq(StringUtils.isNotBlank(gid),Goodtogfs::getGid,gid)
//                    .eq(StringUtils.isNotBlank(gssId),Goodtogfs::getGssid,gssId);
//            GoodtogfsService.remove(GoodtogfsQueryWrapper);
//            /*然后删除商品中的信息*/
////            GoodService.removeById(gid);
//            /*商品不会被删除*/
//        }
        /*删除二级分类信息*/
        return GoodsecondsortService.removeById(gssId);
    }

    /*2.7获取一级分类的个数*/
    @RequestMapping("/getFirstSortNum")
    public Long getFirstSortNum(){
        return GoodfirstsortService.count();
    }

    /*2.8获取二级分类的个数*/
    @RequestMapping("/getSecondSortNum")
    public Long getSecondSortNum(){
        return GoodsecondsortService.count();
    }


    @Autowired
    goodController goodController;



    /*3优惠卷管理*/
    /*3.1.获取所有的优惠卷信息(分页查询) 第几页，一页几条数据*/
    @RequestMapping("/getGoodprePage")
    public List<goodpreWithGoodWithCustomer> getGoodprePage(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        Page<Goodpre> GoodprePage = new Page<>(pageOrder,itemNum);
        Page<Goodpre> page = GoodpreService.page(GoodprePage);
        /*定义返回列表*/
        List<goodpreWithGoodWithCustomer> goodpreWithGoodWithCustomerList = new ArrayList<>();
        for (Goodpre goodpre : page.getRecords()) {
            String gid = goodpre.getGid();
            String cid = goodpre.getCid();
            Good good = GoodService.getById(gid);
            Customer customer = CustomerService.getById(cid);
            goodpreWithGoodWithCustomerList.add(new goodpreWithGoodWithCustomer(goodpre,good,customer));
        }
        return goodpreWithGoodWithCustomerList;
    }

    /*3.11按照优惠卷分类id查询优惠卷信息*/
    @RequestMapping("/getGoodprePageByPreSort")
    public List<goodpreWithGoodWithCustomer> getGoodprePageByPreSort(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum,
            @RequestParam("preSort") String preSort
    ){
        Page<Goodpre> GoodprePage = new Page<>(pageOrder,itemNum);
        LambdaQueryWrapper<Goodpre> GoodpreQueryWrapper = new LambdaQueryWrapper<>();
        GoodpreQueryWrapper
                .eq(StringUtils.isNotBlank(preSort),Goodpre::getPreSort,preSort);
        Page<Goodpre> page = GoodpreService.page(GoodprePage,GoodpreQueryWrapper);
        /*定义返回列表*/
        List<goodpreWithGoodWithCustomer> goodpreWithGoodWithCustomerList = new ArrayList<>();
        for (Goodpre goodpre : page.getRecords()) {
            String gid = goodpre.getGid();
            String cid = goodpre.getCid();
            Good good = GoodService.getById(gid);
            Customer customer = CustomerService.getById(cid);
            goodpreWithGoodWithCustomerList.add(new goodpreWithGoodWithCustomer(goodpre,good,customer));
        }
        return goodpreWithGoodWithCustomerList;
    }



    /*3.2获取优惠卷的个数*/
    @RequestMapping("/getGoodpreNum")
    public Long getGoodpreNum(){
        return GoodpreService.count();
    }

    /*3.21 按照优惠卷分类id查询优惠卷个数*/
    @RequestMapping("/getGoodpreNumBySort")
    public Long getGoodpreNumBySort(
            @RequestParam("preSort") String preSort
    ){
        LambdaQueryWrapper<Goodpre> GoodpreQueryWrapper = new LambdaQueryWrapper<>();
        GoodpreQueryWrapper
                .eq(StringUtils.isNotBlank(preSort),Goodpre::getPreSort,preSort);
        return GoodpreService.count(GoodpreQueryWrapper);
    }

    /*3.3.添加优惠卷信息*/
    @RequestMapping("/addGoodpre")
    public Boolean addGoodpre(
            @RequestParam("gId")String gId,
            @RequestParam("discount")Integer discount,
            @RequestParam("preName")String preName
    ){
        /*获取一共不重复的随机数*/
        String preSort = new Date().toString();
        /*获取所有客户的cId信息*/
        for (Customer customer : CustomerService.list()) {
            String cid = customer.getCid();
            Goodpre Goodpre = new Goodpre(
                    null,
                    cid,
                    gId,
                    discount,
                    preName,
                    0,
                    0,
                    new Date(),
                    preSort
            );
            /*添加进入goodpre*/
            GoodpreService.save(Goodpre);
        }
        return true;
    }

    /*3.4.根据pid删除优惠卷信息*/
    @RequestMapping("/deleteGoodpreByPid")
    public boolean deleteGoodpreByPid(
            @RequestParam("preId")String preId
    ){
        LambdaQueryWrapper<Goodpre> GoodpreQueryWrapper = new LambdaQueryWrapper<>();
        GoodpreQueryWrapper
                .eq(StringUtils.isNotBlank(preId),Goodpre::getPreid,preId);
        return GoodpreService.remove(GoodpreQueryWrapper);
    }

    /*3.41根据preSort删除优惠卷信息*/
    @RequestMapping("/deleteGoodpreByPidBySort")
    public boolean deleteGoodpreByPidBySort(
            @RequestParam("preSort")String preSort
    ){
        LambdaQueryWrapper<Goodpre> GoodpreQueryWrapper = new LambdaQueryWrapper<>();
        GoodpreQueryWrapper
                .eq(StringUtils.isNotBlank(preSort),Goodpre::getPreSort,preSort);
        return GoodpreService.remove(GoodpreQueryWrapper);
    }

    /*3.5.根据id修改优惠卷信息*/
    @RequestMapping("/updateGoodpreByPid")
    public boolean updateGoodpreByPid(
            @RequestParam("preId")String preId,
            @RequestParam("discount")Integer discount,
            @RequestParam("preName")String preName,
            @RequestParam("isInformed")Integer isInformed,
            @RequestParam("isUsed")Integer isUsed
    ){
        LambdaUpdateWrapper<Goodpre> GoodpreUpdateWrapper = new LambdaUpdateWrapper<>();
        GoodpreUpdateWrapper
                .eq(StringUtils.isNotBlank(preId),Goodpre::getPreid,preId)
                .set(Goodpre::getDiscount,discount)
                .set(Goodpre::getPrename,preName)
                .set(Goodpre::getIsinformed,isInformed)
                .set(Goodpre::getIsused,isUsed);
        return GoodpreService.update(GoodpreUpdateWrapper);
    }

    /*3.51根据分类修改优惠卷信息*/
    @RequestMapping("/updateGoodpreByPidBySort")
    public boolean updateGoodpreByPidBySort(
            @RequestParam("preSort")String preSort,
            @RequestParam("discount")Integer discount,
            @RequestParam("preName")String preName,
            @RequestParam("isInformed")Integer isInformed,
            @RequestParam("isUsed")Integer isUsed,
            @RequestParam("gId") String gId

    ){
        LambdaUpdateWrapper<Goodpre> GoodpreUpdateWrapper = new LambdaUpdateWrapper<>();
        GoodpreUpdateWrapper
                .eq(StringUtils.isNotBlank(preSort),Goodpre::getPreSort,preSort)
                .set(Goodpre::getDiscount,discount)
                .set(Goodpre::getPrename,preName)
                .set(Goodpre::getIsinformed,isInformed)
                .set(Goodpre::getIsused,isUsed)
                .set(Goodpre::getGid,gId);
        return GoodpreService.update(GoodpreUpdateWrapper);
    }

    /*4.评论管理*/
    /*4.1.分页查询所有评论信息*/
    @RequestMapping("/getEvaluationPage")
    public List<evaWcusWimgWGood> getEvaluationPage(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        /*返回列表*/
        List<evaWcusWimgWGood> evaluationWithCustomerWithImgList = new ArrayList<>();
        Page<Evaluation> EvaluationPage = new Page<>(pageOrder,itemNum);
        Page<Evaluation> page = EvaluationService.page(EvaluationPage);
        List<Evaluation> evaluationList = page.getRecords();
        /*根据分页*/
        for (Evaluation evaluation : evaluationList) {
            evaluationWithCustomerWithImg egiByEvaluation = customerController.getEGIByEvaluation(evaluation);
            String orderid = egiByEvaluation.getEvaluation().getOrderid();
            /*根据orderId获取gId*/
            Gorder gorder = GorderService.getById(orderid);
            String gid = gorder.getGid();
            /*根据gId获取good*/
            Good good = GoodService.getById(gid);
            evaWcusWimgWGood evaWcusWimgWGood = new evaWcusWimgWGood(
                    egiByEvaluation.getEvaluation(),
                    egiByEvaluation.getCustomer(),
                    egiByEvaluation.getEvaluateimgList(),
                    good
            );
            evaluationWithCustomerWithImgList.add(evaWcusWimgWGood);
        }
        return evaluationWithCustomerWithImgList;
    }

    /*4.2查询一共有多少条评论信息*/
    @RequestMapping("/getEvaluationNum")
    public Long getEvaluationNum(){
        return EvaluationService.count();
    }


    /*4.3根据评论id修改评论信息*/
    @RequestMapping("/updateEvaluation")
    public Boolean updateEvaluation(
            @RequestParam("evaluationId")String evaluationId,
            @RequestParam("evaluation")String evaluation
    ){
        LambdaUpdateWrapper<Evaluation> EvaluationUpdateWrapper = new LambdaUpdateWrapper<>();
        EvaluationUpdateWrapper
                .eq(StringUtils.isNotBlank(evaluationId),Evaluation::getEvaluationid,evaluationId)
                .set(Evaluation::getEvaluationContent,evaluation);
        return EvaluationService.update(EvaluationUpdateWrapper);
    }

    @Autowired
    EvaluateimgService EvaluateimgService;


    /*4.4根据评论id删除评论信息*/
    @RequestMapping("/deleteEvaluation")
    public Boolean deleteEvaluation(
            @RequestParam("evaluationId")String evaluationId
    ){
        LambdaQueryWrapper<Evaluateimg> EvaluateimgQueryWrapper = new LambdaQueryWrapper<>();
        EvaluateimgQueryWrapper
                .eq(StringUtils.isNotBlank(evaluationId),Evaluateimg::getEvaluationid,evaluationId);
        EvaluateimgService.remove(EvaluateimgQueryWrapper);
        /*删除对应评价信息*/
        return EvaluationService.removeById(evaluationId);
    }

    /*5订单管理*/
    /*5.1.分页查询所有订单信息*/
    @RequestMapping("/getGorderPage")
    public List<gorderWithGoodWithCustomer> getGorderPage(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        List<gorderWithGoodWithCustomer> ObjectList = new ArrayList<>();
        /*订单信息，客户信息，商品信息*/
        Page<Gorder> GorderPage = new Page<>(pageOrder,itemNum);
        Page<Gorder> Gorderpage = GorderService.page(GorderPage);
        List<Gorder> gorderList = Gorderpage.getRecords();
        for (Gorder gorder : gorderList) {
            String gid = gorder.getGid();
            Good good = GoodService.getById(gid);
            String cId = gorder.getCid();
            Customer customer = CustomerService.getById(cId);
            gorderWithGoodWithCustomer ObjectArray = new gorderWithGoodWithCustomer(gorder,good,customer);
            ObjectList.add(ObjectArray);
        }
        return ObjectList;
    }

    /*5.2获取一共有多少条订单信息*/
    @RequestMapping("/getGorderNum")
    public Long getGorderNum(){
        return GorderService.count();
    }

    @Autowired
    GimgService GimgService;

    /*6图像管理*/
    /*6.1根据gId获取图像信息*/
    /*分页查询*/
    @RequestMapping("/getAllGoodWithGImg")
    public List<goodWithGImg> getAllGoodWithGImg(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        /*返回列表*/
        List<goodWithGImg> goodWithGImgList = new ArrayList<>();
        /*首先要获取所有的gId*/
        Page<Good> GoodPage = new Page<>(pageOrder,itemNum);
        Page<Good> page = GoodService.page(GoodPage);
        List<Good> goodList = page.getRecords();
        for (Good good : goodList) {
            /*根据gId获取GImg*/
            String gid = good.getGid();
            LambdaQueryWrapper<Gimg> GimgQueryWrapper = new LambdaQueryWrapper<>();
            GimgQueryWrapper
                    .eq(StringUtils.isNotBlank(gid),Gimg::getGid,gid);
            List<Gimg> gimgList = GimgService.list(GimgQueryWrapper);
            com.example.pro1.entity.lian.pojoMid.goodWithGImg goodWithGImg2 = new goodWithGImg(
                    gimgList,
                    good
            );
            goodWithGImgList.add(goodWithGImg2);
        }
        return goodWithGImgList;
    }

    /*6.2.根据gImgId修改图像信息*/
    @RequestMapping("/updateGimg")
    public Boolean updateGimg(
            @RequestParam("gImgId")String gImgId,
            @RequestParam("gImgSrc")String gImgSrc
    ){
        LambdaUpdateWrapper<Gimg> GimgUpdateWrapper = new LambdaUpdateWrapper<>();
        GimgUpdateWrapper
                .eq(StringUtils.isNotBlank(gImgId),Gimg::getGimgid,gImgId)
                .set(Gimg::getGimgsrc,gImgSrc);
        return GimgService.update(GimgUpdateWrapper);
    }

    /*6.3.根据gId增加图像信息*/
    @RequestMapping("/addGimg")
    public Boolean addGimg(
            @RequestParam("gId")String gId,
            @RequestParam("gImgSrc")String gImgSrc
    ){
        Gimg Gimg1 = new Gimg(
                null,
                gId,
                gImgSrc
        );
        return GimgService.save(Gimg1);
    }


    /*6.4.根据gImgId删除图像信息*/
    @RequestMapping("/deleteGimg")
    public Boolean deleteGimg(
            @RequestParam("gImgId")String gImgId
    ){
        return GimgService.removeById(gImgId);
    }


    @Autowired
    RootService RootService;
    /*7.管理员信息管理*/

    /*7.1管理员信息获取*/
    @RequestMapping("/getRootInfoPage")
    public List<Root> getRootInfoPage(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        Page<Root> RootPage = new Page<>(pageOrder,itemNum);
        Page<Root> page = RootService.page(RootPage);
        return page.getRecords();
    }

    /*7.2一共有多少个管理员*/
    @RequestMapping("/getRootNum")
    public Long getRootNum(){
        return RootService.count();
    }


    /*7.3 增加管理员*/
    @RequestMapping("/addRoot")
    public Boolean addRoot(
            @RequestParam("rName")String rName,
            @RequestParam("rPassword")String rPassword,
            @RequestParam("rSex")String rSex,
            @RequestParam("rPhone")String rPhone,
            @RequestParam("rEmail")String rEmail,
            @RequestParam("rWXNumber")String rWXNumber,
            @RequestParam("rImageSrc")String rImageSrc
    ){
        Root root = new Root(
            null,
                rName,
                rPassword,
                rSex,
                rPhone,
                rEmail,
                rWXNumber,
                rImageSrc
        );
        return RootService.save(root);
    }

    /*7.4 删除管理员*/
    @RequestMapping("/deleteRoot")
    public Boolean deleteRoot(
            @RequestParam("rId")String rId
    ){
        return RootService.removeById(rId);
    }

    /*7.5 修改管理员*/
    @RequestMapping("/updateRoot")
    public Boolean updateRoot(
            @RequestParam("rId")String rId,
            @RequestParam("rName")String rName,
            @RequestParam("rPassword")String rPassword,
            @RequestParam("rSex")String rSex,
            @RequestParam("rPhone")String rPhone,
            @RequestParam("rEmail")String rEmail,
            @RequestParam("rWXNumber")String rWXNumber,
            @RequestParam("rImageSrc")String rImageSrc
    ){
        LambdaUpdateWrapper<Root> RootUpdateWrapper = new LambdaUpdateWrapper<>();
        RootUpdateWrapper
                .eq(StringUtils.isNotBlank(rId),Root::getRid,rId)
                .set(Root::getRname,rName)
                .set(Root::getRpassword,rPassword)
                .set(Root::getRsex,rSex)
                .set(Root::getRphone,rPhone)
                .set(Root::getRemail,rEmail)
                .set(Root::getRwxnumber,rWXNumber)
                .set(Root::getRimagesrc,rImageSrc);
        return RootService.update(RootUpdateWrapper);
    }

    /*还没有添加*/
    /*8.1查询客户信息(分页)*/
    @RequestMapping("/getCustomerInfoPage")
    public List<Customer> getCustomerInfoPage(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        Page<Customer> customerPage = new Page<>(pageOrder,itemNum);
        Page<Customer> page = CustomerService.page(customerPage);
        return page.getRecords();
    }



    /*8.2一共有多少客户信息*/
    @RequestMapping("/getCustomerNum")
    public Long getCustomerNum(){
        return CustomerService.count();
    }

    /*8.3根据客户id删除客户信息*/
    @RequestMapping("/deleteCustomerById")
    public Boolean deleteCustomerById(
            @RequestParam("cId")String cId
    ){
        return CustomerService.removeById(cId);
    }

    /*8.4修改客户信息*/
    @RequestMapping("/updateCustomer")
    public Boolean updateCustomer(
            @RequestParam("cId")String cId,
            @RequestParam("cName")String cName,
            @RequestParam("cEmail")String cEmail,
            @RequestParam("cPassword")String cPassword,
            @RequestParam("cSex")String cSex,
            @RequestParam("cWXNumber")String cWXNumber,
            @RequestParam("cWXOpenId")String cWXOpenId,
            @RequestParam("cImgSrc")String cImgSrc
    ){
        LambdaUpdateWrapper<Customer> CustomerUpdateWrapper = new LambdaUpdateWrapper<>();
        CustomerUpdateWrapper
                .eq(StringUtils.isNotBlank(cId),Customer::getCid,cId)
                .set(Customer::getCname,cName)
                .set(Customer::getCemail,cEmail)
                .set(Customer::getCpassword,cPassword)
                .set(Customer::getCsex,cSex)
                .set(Customer::getCwxnumber,cWXNumber)
                .set(Customer::getCwxopenid,cWXOpenId)
                .set(Customer::getCimgsrc,cImgSrc);
        return CustomerService.update(CustomerUpdateWrapper);
    }


    /*9模糊查询*/
    /*9.1根据用户名进行模糊查询*/
    @RequestMapping("/getCustomerLike")
    public List<Customer> getCustomerLike(
            @RequestParam("cName")String cName,
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        Page<Customer> CustomerPage = new Page<>(pageOrder,itemNum);
        LambdaQueryWrapper<Customer> CustomerQueryWrapper = new LambdaQueryWrapper<>();
        CustomerQueryWrapper.like(StringUtils.isNotBlank("cName"),Customer::getCname,cName);
        return CustomerService.page(CustomerPage,CustomerQueryWrapper).getRecords();
    }

    /*9.2根据用户名对管理员进行模糊查询*/
    @RequestMapping("/getRootLike")
    public List<Root> getRootLike(
            @RequestParam("rName")String rName,
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        Page<Root> RootPage = new Page<>(pageOrder,itemNum);
        LambdaQueryWrapper<Root> RootQueryWrapper = new LambdaQueryWrapper<>();
        RootQueryWrapper.like(StringUtils.isNotBlank(rName),Root::getRname,rName);
        return RootService.page(RootPage, RootQueryWrapper).getRecords();
    }

    /*9.3根据订单号查询订单信息*/
    @RequestMapping("/getGorderWithGoodWithCustomerByOrderId")
    public List<gorderWithGoodWithCustomer> getGorderWithGoodWithCustomerByOrderId(
            @RequestParam("orderId")String orderId
    ){
        LambdaQueryWrapper<Gorder> GorderQueryWrapper = new LambdaQueryWrapper<>();
        GorderQueryWrapper
                .eq(StringUtils.isNotBlank(orderId),Gorder::getOrderid,orderId);
        List<gorderWithGoodWithCustomer> ObjectList = new ArrayList<>();
        /*订单信息，客户信息，商品信息*/;
        List<Gorder> gorderList = GorderService.list(GorderQueryWrapper);
        for (Gorder gorder : gorderList) {
            String gid = gorder.getGid();
            Good good = GoodService.getById(gid);
            String cId = gorder.getCid();
            Customer customer = CustomerService.getById(cId);
            gorderWithGoodWithCustomer ObjectArray = new gorderWithGoodWithCustomer(gorder,good,customer);
            ObjectList.add(ObjectArray);
        }
        return ObjectList;
    }


    /*9.4根据商品名称获取评论信息*/
    @RequestMapping("/getEvaWcusWimgWGoodByGName")
    public List<evaWcusWimgWGood> getEvaWcusWimgWGoodByGName(
            @RequestParam("gName") String gName,
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        /*返回列表*/
        Page<Good> GoodPage = new Page<>(pageOrder,itemNum);
        List<evaWcusWimgWGood> evaluationWithCustomerWithImgList = new ArrayList<>();
        LambdaQueryWrapper<Good> GoodQueryWrapper = new LambdaQueryWrapper<>();
        GoodQueryWrapper
                .like(StringUtils.isNotBlank(gName),Good::getGname,gName);
        List<Good> goodList = GoodService.page(GoodPage,GoodQueryWrapper).getRecords();
        for (Good good : goodList) {
            List<Evaluation> evaluationList = customerController
                    .getEvaluationListByGId(good.getGid());
            for (Evaluation evaluation : evaluationList) {
                evaluationWithCustomerWithImg egiByEvaluation = customerController.getEGIByEvaluation(evaluation);
                String orderid = egiByEvaluation.getEvaluation().getOrderid();
                /*根据orderId获取gId*/
                Gorder gorder = GorderService.getById(orderid);
                String gid = gorder.getGid();
                /*根据gId获取good*/
                Good good1 = GoodService.getById(gid);
                evaWcusWimgWGood evaWcusWimgWGood = new evaWcusWimgWGood(
                        egiByEvaluation.getEvaluation(),
                        egiByEvaluation.getCustomer(),
                        egiByEvaluation.getEvaluateimgList(),
                        good1
                );
                evaluationWithCustomerWithImgList.add(evaWcusWimgWGood);
            }
        }
        return evaluationWithCustomerWithImgList;

    }


    /*9.5根据优惠卷名称获取优惠卷信息*/
    @RequestMapping("/getGoodPreLike")
    public List<goodpreWithGoodWithCustomer> getGoodPreLike(
            @RequestParam("preName")String preName,
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        Page<Goodpre> GoodprePage = new Page<>(pageOrder,itemNum);
        LambdaQueryWrapper<Goodpre> GoodpreQueryWrapper = new LambdaQueryWrapper<>();
        GoodpreQueryWrapper
                .like(StringUtils.isNotBlank(preName),Goodpre::getPrename,preName);
        List<Goodpre> goodpreList = GoodpreService.page(GoodprePage,GoodpreQueryWrapper).getRecords();
        /*定义返回列表*/
        List<goodpreWithGoodWithCustomer> goodpreWithGoodWithCustomerList = new ArrayList<>();
        for (Goodpre goodpre : goodpreList) {
            String gid = goodpre.getGid();
            String cid = goodpre.getCid();
            Good good = GoodService.getById(gid);
            Customer customer = CustomerService.getById(cid);
            goodpreWithGoodWithCustomerList.add(new goodpreWithGoodWithCustomer(goodpre,good,customer));
        }
        return goodpreWithGoodWithCustomerList;
    }

    /*10.1返回所有的二级分类信息，顺便返回一级分类信息*/
    @RequestMapping("/getSecondSortWithFirstSort")
    public List<firstWithSecond> getSecondSortWithFirstSort(
            @RequestParam("pageOrder") Integer pageOrder,
            @RequestParam("itemNum") Integer itemNum
    ){
        /*创建返回列表*/
        List<firstWithSecond> firstWithSecondList = new ArrayList<>();
        /*获取所有的二级分类信息*/
        Page<Goodsecondsort> GoodsecondsortPage = new Page<>(pageOrder,itemNum);
        List<Goodsecondsort> goodSecondSortList = GoodsecondsortService.page(GoodsecondsortPage).getRecords();
        for (Goodsecondsort goodsecondsort : goodSecondSortList) {
            /*根据二级分类信息获取一级分类信息*/
            String gssid = goodsecondsort.getGssid();
            /*根据gssId查询一级分类信息*/
            LambdaQueryWrapper<Gsstogfs> GsstogfsQueryWrapper = new LambdaQueryWrapper<>();
            GsstogfsQueryWrapper.eq(StringUtils.isNotBlank(gssid),Gsstogfs::getGssid,gssid);
            Gsstogfs gsstogfs = GsstogfsService.getOne(GsstogfsQueryWrapper);
            String gfsid = gsstogfs.getGfsid();
            Goodfirstsort goodfirstsort = GoodfirstsortService.getById(gfsid);
            firstWithSecond firstWithSecond = new firstWithSecond(goodfirstsort,goodsecondsort);
            firstWithSecondList.add(firstWithSecond);
        }
        return firstWithSecondList;
    }


    /*10.2返回所有商品的商品名与库存*/
    @RequestMapping("/returnGNameWithGNum")
    public List<gNameWithGNum> returnGNameWithGNum(){
        LambdaQueryWrapper<Good> GoodQueryWrapper = new LambdaQueryWrapper<>();
        GoodQueryWrapper.ne(true,Good::getGnumber,"-1");
        List<gNameWithGNum> gNameWithGNumList = new ArrayList<>();
        /*查询所有商品信息*/
        for (Good good : GoodService.list(GoodQueryWrapper)) {
            gNameWithGNum gNameWithGNum = new gNameWithGNum(
                    good.getGname(),
                    good.getGnumber()
            );
            gNameWithGNumList.add(gNameWithGNum);
        }
        return gNameWithGNumList;
    }
}
